package com.typy.www.tpay.ui.itemview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.bean.vm.BankCardVm;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.util.LocalDisplay;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/28.
 */

public class BankCardItemView {
    private Context mContext;
    private View mView;
    ViewHolder holder;

    public BankCardItemView(Context context) {
        mContext = context;
        mView = LayoutInflater.from(context).inflate(R.layout.item_bandcard, null);
    }

    public View getView() {
        return mView;
    }

    public void bind(final BankCardVm vm, int position, final int classType) {
        holder = new ViewHolder(mView);
        Drawable drawable = mContext.getResources().getDrawable(R.drawable.checkbox_select);
        drawable.setBounds(0, 0, 40, 40);
        holder.cbSheweimoren.setCompoundDrawables(drawable, null, null, null);
        UpdataUiUtil.getDefaul().setText(vm.getFdBankName(), holder.tvBankName)
                .setText(vm.getFdBankUserName(), holder.tvCountName)
                .setText(vm.getFdBankNo(), holder.tvCountNum);
        if (classType == 0) {
            holder.cbSheweimoren.setVisibility(View.VISIBLE);
            holder.tvDel.setVisibility(View.VISIBLE);
            holder.tvMoren.setVisibility(View.GONE);
        } else {
            holder.llCardhold.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!vm.getFdStatus().equals("1")) {
                        EventBus.getDefault().post(new MessageEvent(MessageAction.DEFAULBANK, vm.getFdId(), "1",classType+""));
                    }
                }
            });
            holder.cbSheweimoren.setVisibility(View.GONE);
            holder.tvDel.setVisibility(View.GONE);
            holder.tvMoren.setVisibility(vm.getFdStatus().equals("1") ? View.VISIBLE : View.GONE);
            if (!vm.getFdStatus().equals("1")) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LocalDisplay.dp2px(10));
                holder.llMoren.setLayoutParams(params);
            } else {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LocalDisplay.dp2px(35));
                holder.llMoren.setLayoutParams(params);
            }
        }
        holder.cbSheweimoren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.cbSheweimoren.isChecked()) {
                    EventBus.getDefault().post(new MessageEvent(MessageAction.DEFAULBANK, vm.getFdId(), "1"));
                } else {
                    EventBus.getDefault().post(new MessageEvent(MessageAction.DEFAULBANK, vm.getFdId(), "0"));
                }
            }
        });
        holder.cbSheweimoren.setChecked(vm.getFdStatus().equals("1") ? true : false);
        holder.tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent(MessageAction.DELBANK, vm.getFdId()));
            }
        });

    }


    static class ViewHolder {
        @BindView(R.id.tv_bankName)
        TextView tvBankName;
        @BindView(R.id.tv_countName)
        TextView tvCountName;
        @BindView(R.id.tv_countNum)
        TextView tvCountNum;
        @BindView(R.id.cb_sheweimoren)
        CheckBox cbSheweimoren;
        @BindView(R.id.tv_moren)
        TextView tvMoren;
        @BindView(R.id.tv_del)
        TextView tvDel;
        @BindView(R.id.ll_moren)
        LinearLayout llMoren;
        @BindView(R.id.ll_cardhold)
        PercentLinearLayout llCardhold;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
